package com.etop.learnapp.activity;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.etop.learnapp.R;
import com.etop.learnapp.fragment.Basic_Info_Fragment;
import com.etop.learnapp.fragment.Student_List_Fragment;
import com.etop.learnapp.fragment.Teacher_List_Fragment;
import com.etop.learnapp.personalClass.Basic_Info;
import com.etop.learnapp.personalClass.Student;
import com.etop.learnapp.personalClass.Teacher;

import java.util.ArrayList;


public class ApplyActivity extends AppCompatActivity implements View.OnClickListener{

    private Toolbar toolbar;
    Fragment fgBasicInfo,fgTeacherList,fgTeacherInfo;
    private static final int LIST_LENTH = 100;

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    private int currentPage = 1;
    public Basic_Info basicInfos = new Basic_Info();
    public ArrayList<Student> studentArrayList = new ArrayList<Student>();
    public ArrayList<Teacher> teacherArrayList = new ArrayList<Teacher>();

    public Teacher_List_Fragment teacher_list_fragment = new Teacher_List_Fragment();

    public Student_List_Fragment student_list_fragment = new Student_List_Fragment();

    public Basic_Info_Fragment basic_info_fragment = new Basic_Info_Fragment();

    FragmentManager fragmentManager = getFragmentManager();

    public void ExtendTeacherList(){
        for(int i = 0; i< LIST_LENTH;i++){
            teacherArrayList.add(new Teacher());
        }
    }

    public void ExtendStudentList(){
        for(int i = 0; i< LIST_LENTH;i++){
            studentArrayList.add( new Student());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        toolbar = (Toolbar) findViewById(R.id.toolbar_include);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
                }
            });
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fl_Main, basic_info_fragment);
            fragmentTransaction.commit();

        ExtendTeacherList();
        ExtendStudentList();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if(currentPage == 2){
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_Main, basic_info_fragment);
                fragmentTransaction.addToBackStack(null);//加入模拟栈
                fragmentTransaction.commit();
            }

            else if(currentPage == 3){
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_Main, teacher_list_fragment);
                fragmentTransaction.addToBackStack(null);//加入模拟栈
                fragmentTransaction.commit();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {

        if (v == basic_info_fragment.btnNext1){
            //基本信息到教师
            currentPage = 2;
            basic_info_fragment.saveInfo();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fl_Main, teacher_list_fragment);
            fragmentTransaction.addToBackStack(null);//加入模拟栈
            fragmentTransaction.commit();
        }

        else if (v == teacher_list_fragment.btnNext2){
            //教师到学生
            currentPage = 3;
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fl_Main, student_list_fragment);
            fragmentTransaction.addToBackStack(null);//加入模拟栈
            fragmentTransaction.commit();
        }

        else if (v == teacher_list_fragment.btn_techerListBack){
            currentPage = 1;
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fl_Main, basic_info_fragment);
            fragmentTransaction.addToBackStack(null);//加入模拟栈
            fragmentTransaction.commit();
        }

        else if (v == student_list_fragment.btn_studentListBack){
            currentPage = 2;
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fl_Main, teacher_list_fragment);
            fragmentTransaction.addToBackStack(null);//加入模拟栈
            fragmentTransaction.commit();
        }
        else if(v==student_list_fragment.btnComplete){
            //    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
            AlertDialog.Builder builder = new AlertDialog.Builder(ApplyActivity.this);
            //    设置Title的图标
            builder.setIcon(R.drawable.ic_verified_user_black_48dp);
            //    设置Title的内容
            builder.setTitle("提交确认");
            //    设置Content来显示一个信息
            builder.setMessage("确定提交报名信息？");
            //    设置一个PositiveButton
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(ApplyActivity.this, "完成报名", Toast.LENGTH_SHORT).show();
                    finish();
                }
            });
            //    设置一个NegativeButton
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    Toast.makeText(ApplyActivity.this, "继续填写", Toast.LENGTH_SHORT).show();
                }
            });

            //    显示出该对话框
            builder.show();
        }
        }





    @Override
    public void onBackPressed() {
        if(currentPage == 1){

            //    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
            AlertDialog.Builder builder = new AlertDialog.Builder(ApplyActivity.this);
            //    设置Title的图标
            builder.setIcon(R.drawable.ic_report_problem_black_48dp);
            //    设置Title的内容
            builder.setTitle("退出确认");
            //    设置Content来显示一个信息
            builder.setMessage("确定退出报名？");
            //    设置一个PositiveButton
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(ApplyActivity.this, "退出报名", Toast.LENGTH_SHORT).show();
                    finish();
                }
            });
            //    设置一个NegativeButton
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(ApplyActivity.this, "继续填写", Toast.LENGTH_SHORT).show();
                }
            });

            //    显示出该对话框
            builder.show();


        }
        else if(currentPage == 2){
            currentPage = 1;
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fl_Main, basic_info_fragment);
            fragmentTransaction.addToBackStack(null);//加入模拟栈
            fragmentTransaction.commit();
        }
        else if(currentPage == 3){
            currentPage = 2;
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fl_Main, teacher_list_fragment);
            fragmentTransaction.addToBackStack(null);//加入模拟栈
            fragmentTransaction.commit();
        }
    }
}

